package com.inteligencia.stage.services.mail.impl;

import com.inteligencia.stage.services.mail.MailService;
import com.inteligencia.stage.services.mail.dto.MailServiceParametersDTO;
import com.inteligencia.stage.services.mail.exception.MailServiceException;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class JavaMailServiceImpl extends MailService {

    private String hostname;
    private String port;
    private String username;
    private String password;
    private String nicksender;

    @Override
    public void initialize(Object param) {
    }

    public void setParameters(MailServiceParametersDTO parameters) {
        if (parameters.getHostname() != null && parameters.getHostname().trim().length() > 0) {
            this.hostname = parameters.getHostname();
        }
        if (parameters.getNicksender() != null && parameters.getNicksender().trim().length() > 0) {
            this.nicksender = parameters.getNicksender();
        }
        if (parameters.getPassword() != null && parameters.getPassword().trim().length() > 0) {
            this.password = parameters.getPassword();
        }
        if (parameters.getPort() != null && parameters.getPort().trim().length() > 0) {
            this.port = parameters.getPort();
        }
        if (parameters.getUsername() != null && parameters.getUsername().trim().length() > 0) {
            this.username = parameters.getUsername();
        }
    }

    public void send(String senderAddress, String toAddress, String cc,
            String bcc, String subject, String msgTxt)
            throws MailServiceException {
        try {
            MimeMessage msg = new MimeMessage(getSession());
            msg.setFrom(new InternetAddress(senderAddress));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));
            if (cc != null && cc.length() > 0) {
                msg.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
            }
            if (bcc != null && bcc.length() > 0) {
                msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc));
            }

            msg.setSubject(subject);
            msg.setText(msgTxt);
            sendMessage(msg);
        } catch (AddressException ex) {
            ex.printStackTrace();
            throw new MailServiceException("Mail Service Exception");
        } catch (MessagingException ex) {
            ex.printStackTrace();
            throw new MailServiceException("Mail Service Exception");
        }
    }

    //habilidao para enviar HTML
    public void send(String senderAddress, String toAddress, String cc,
            String bcc, String subject, Multipart multipart)
            throws MailServiceException {
        try {
            MimeMessage msg = new MimeMessage(getSession());
            msg.setFrom(new InternetAddress(senderAddress));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));
            if (cc != null && cc.length() > 0) {
                msg.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
            }
            if (bcc != null && bcc.length() > 0) {
                msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc));
            }

            msg.setSubject(subject);
            msg.setContent(multipart);
//            msg.setText(msgTxt);
            sendMessage(msg);
        } catch (AddressException ex) {
            ex.printStackTrace();
            throw new MailServiceException("Mail Service Exception");
        } catch (MessagingException ex) {
            ex.printStackTrace();
            throw new MailServiceException("Mail Service Exception");
        }
    }

    public void sendHtml(String senderAddress, String toAddress, String cc,
            String bcc, String subject, String htmlText)
            throws MailServiceException {
        try {
            MimeMessage msg = new MimeMessage(getSession());
            msg.setFrom(new InternetAddress(senderAddress));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));
            if (cc != null && cc.length() > 0) {
                msg.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
            }
            if (bcc != null && bcc.length() > 0) {
                msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc));
            }

            msg.setSubject(subject);
            msg.setContent(htmlText, "text/html; charset=ISO-8859-1");
            sendMessage(msg);
        } catch (AddressException ex) {
            ex.printStackTrace();
            throw new MailServiceException("Mail Service Exception");
        } catch (MessagingException ex) {
            ex.printStackTrace();
            throw new MailServiceException("Mail Service Exception");
        }
    }

    @SuppressWarnings("static-access")
    public void sendMessage(MimeMessage message) throws MessagingException {
        Transport transp = getSession().getTransport();
        transp.connect();
        transp.send(message);
        transp.close();
    }

    public Session getSession() {
        Properties props = new Properties();
        //props.put("mail.host", hostname);
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.host", hostname);
        props.setProperty("mail.port", port);
        props.setProperty("mail.user", username);
        props.setProperty("mail.password", password);
        props.setProperty("mail.smtp.host", hostname);
        props.setProperty("mail.smtp.port", port);
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.mime.base64.ignoreerrors", "true");
        SMTPAuthenticator auth = new SMTPAuthenticator(username, password);
        Session session = Session.getDefaultInstance(props, auth);
        return session;
    }
}
